/* *****************************************************************************************
 
Made by www.Flashphoner.com, all rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) � 
please contact support@flashphoner.com 

******************************************************************************************/

package com.flashphoner.api.management
{
	import flash.media.Microphone;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundCodec;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	
	public class SoundControl
	{
		
		[Embed(source="/sounds/CALL_OUT.mp3")]
		private static var ringClass:Class;		
		[Embed(source="/sounds/BUSY.mp3")]
		private static var busyClass:Class;
		[Embed(source="/sounds/REGISTER.mp3")]
		private static var registerClass:Class;
		[Embed(source="/sounds/HANGUP.mp3")]
		private static var finishClass:Class;

		private var ringSound:Sound;		
		private var busySound:Sound;
		private var registerSound:Sound;
		private var finishSound:Sound;
		private var ringSoundChannel:SoundChannel;

		/**
		 * Path to sound for ring
		 **/		
		public static var RING_SOUND:String = null;
		/**
		 * Path to sound for busy
		 **/		
		public static var BUSY_SOUND:String = null;
		/**
		 * Path to sound for register event on voip server
		 **/		
		public static var REGISTER_SOUND:String = null;
		/**
		 * Path to sound for finish call
		 **/		
		public static var FINISH_SOUND:String = null;
				
		private static var soundControl:SoundControl;
		
		private var mic:Microphone;
		
		/**
		 * Control class (singelton) for microphone and sounds.
		 **/
		public function SoundControl()
		{
			mic = Microphone.getMicrophone();		
			mic.codec = SoundCodec.SPEEX;
			mic.framesPerPacket = 1;
			mic.rate = 16;
			mic.gain = 50;
			mic.encodeQuality = 6;
			mic.soundTransform = new SoundTransform(1,0);			
			mic.setLoopBack(false);			
			mic.setSilenceLevel(0,3600000);
			mic.setUseEchoSuppression(true);
		}
		
		/**
		 * Get instance of singelton
		 **/
		public static function getInstance():SoundControl{
			if (soundControl == null){
				soundControl = new SoundControl();
				soundControl.ringSound = Sound(new ringClass());			
				soundControl.busySound = Sound(new busyClass());
				soundControl.registerSound = Sound(new registerClass());
				soundControl.finishSound = Sound(new finishClass());	    
			}
			return soundControl;
		}
		
	    /**
		 * Update all sounds from pathes
		 **/
	    public function updateSounds():void{
	    	if (SoundControl.RING_SOUND != null){
				ringSound = new Sound(new URLRequest(SoundControl.RING_SOUND));
	    	}
	    	if (SoundControl.BUSY_SOUND != null){
				busySound = new Sound(new URLRequest(SoundControl.BUSY_SOUND));
	    	}
	    	if (SoundControl.REGISTER_SOUND != null){
				registerSound = new Sound(new URLRequest(SoundControl.REGISTER_SOUND));
	    	}
	    	if (SoundControl.FINISH_SOUND != null){
				finishSound = new Sound(new URLRequest(SoundControl.FINISH_SOUND));
	    	}
	    }

		/**
		 * Play busy sound
		 **/
		public function playBusySound():void{
			busySound.play(0,1);			
		}

		/**
		 * Play finish sound
		 **/
		public function playFinishSound():void{
			finishSound.play(0,1);		
		}
		
		/**
		 * Play register sound
		 **/
		public function playRegisterSound():void{
			registerSound.play(0,1);		
		}			
		
		/**
		 * Stop ring sound
		 **/
		public function stopRingSound():void{
			if (ringSoundChannel!=null){
				ringSoundChannel.stop();
				ringSoundChannel = null;
			}
		}
		
		/**
		 * Play register sound
		 **/		
		public function playRingSound():void{
			if (ringSoundChannel == null){
				ringSoundChannel = ringSound.play(0,999);
			}			
		}
		
		/**
		 * Change current microphone
		 * @param microphone new current microphone
		 * @param isLoopBack playback voice on speakers
		 * @param gain volume of microphone(-1 - if not change volume)
		 **/
		public function changeMicrophone(microphone:Microphone,isLoopback:Boolean,gain:Number = -1):void{
			var oldGain:int = this.mic.gain;
			this.mic = microphone;
			if (this.mic != null){
				mic.codec = SoundCodec.SPEEX;
				mic.framesPerPacket = 1;
				mic.rate = 16;
				if (gain != -1){
					mic.gain = gain;
				}else{
					mic.gain = oldGain;
				}
				mic.encodeQuality = 6;
				mic.soundTransform = new SoundTransform(1,0);			
				mic.setLoopBack(isLoopback);			
				mic.setSilenceLevel(0,3600000);
				mic.setUseEchoSuppression(true);
			}
		}
		
		/**
		 * Get cuurent microphone
		 **/
		public function getMicrophone():Microphone{
			return mic;
		}		
		

	}
}